<?php
class CliMonkeyController extends BpfController
{
  public function pickupCLI()
  {
    $playerModel = $this->getModel('player');
    $mysqlModel = $this->getModel('mysql');
    do {
     $players = $mysqlModel->getSqlBuilder()
       ->from('players')->join('stores', 'players.player_id = stores.store_id')
       ->select('players.*')->where('stores.monkey_appear', 1)
       ->where('stores.monkey_pickup_time <', REQUEST_TIME - 3600)
       ->limitPage(100, 1)->query()->all('Player');
     foreach ($players as $player) {
       if ($mysqlModel->query('UPDATE `stores` SET `monkey_pickup_time` = ? WHERE `store_id` = ?', [
         REQUEST_TIME, $player->player_id
       ])->affectedRows()) {
        $player->chageFlowers('猴子偷花', -min($player->flowers, 5) , '猴子来啦');
       }
     }
    } while(false);
  }

  public function appearCLI()
  {
    $this->getModel('mysql')
      ->query('UPDATE `stores` set `monkey_appear` = 1 WHERE `monkey_appear` = 0 AND `monkey_disappear_time` < ?', REQUEST_TIME - 14400);
  }
}
